{extend name="common@base/common" /}

{block name="style"}
<link href="__CSS__/main.min.css" rel="stylesheet" type="text/css"/>
{/block}

{block name="body"}
<div class="container">
  <ol class="breadcrumb">
      <li><a href="{:url('index/index')}">演示首页</a></li>
      <li>对话框触发器</li>
  </ol>
<section class="demo-section">
<div id="pageContent">

<article><p>对话框触发器允许你不需要书写静态对话框HTML，直接使用触发按钮或者一行Javascript代码即可让一个全新的对话框展现。支持使用Ajax从远程获取内容，或者通过iframe加载任何页面内容，当然不使用远程内容，直接使用本地内容也是很方便。</p></article>





<section><header><h3>加载远程内容</h3></header><article><p>这里提供两种方法（Ajax和iframe）来加载远程内容，使用起来几乎似乎没有任何区别，你只需要确保远程地址所提供的内容是正确的类型。</p><p>在获取远程内容到显示的过程中，会显示正在加载的图标动画。</p><h4>使用Ajax</h4><p>使用<code>data-remote="(ajax get url)"</code>属性来指定ajax片段获取地址。或者同时指定<code>data-type="ajax"</code>和<code>data-url="(ajax get url)"</code>属性。</p><div class="example">
  <button type="button" class="btn btn-primary" data-remote="{:url('remote_modal')}" data-toggle="modal">Ajax对话框</button>

</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="com">&lt;!-- 使用data-remote属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-remote</span><span class="pun">=</span><span class="atv">"partial/remote-modal.html"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">Ajax对话框</span><span class="tag">&lt;/button&gt;</span><span class="pln">

</span><span class="com">&lt;!-- 使用data-type 和data-url属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-type</span><span class="pun">=</span><span class="atv">"ajax"</span><span class="pln"> </span><span class="atn">data-url</span><span class="pun">=</span><span class="atv">"partial/remote-modal.html"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">Ajax对话框≶/button&gt;</span></code></pre><p>在返回的ajax片段中，你可以包含一个完整的<code>.modal-dialog</code>内容，或者仅包含<code>.modal-content</code>，甚至不是任何静态modal结构，触发器会根据所包含的内容自动补全对话框的缺失部分。</p><h4>使用iframe（内嵌框架）</h4><p>使用<code>data-iframe="(iframe url)"</code>属性来指定远程页面内容获取地址。或者同时指定<code>data-type="iframe"</code>和<code>data-url="(iframe url)"</code>属性。</p><div class="example">

  <button type="button" class="btn btn-primary" data-waittime="5000" data-iframe="{:url('iframe_modal')}" data-toggle="modal" data-loading-icon="icon-spinner-snake" data-height="300">iframe对话框</button>

</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="com">&lt;!-- 使用data-iframe属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-iframe</span><span class="pun">=</span><span class="atv">"partial/iframe-modal.html"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">iframe对话框</span><span class="tag">&lt;/button&gt;</span><span class="pln">

</span><span class="com">&lt;!-- 使用data-type 和data-url属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-type</span><span class="pun">=</span><span class="atv">"iframe"</span><span class="pln"> </span><span class="atn">data-url</span><span class="pun">=</span><span class="atv">"partial/iframe-modal.html"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">iframe对话框≶/button&gt;</span></code></pre></article></section>



<section><header><h3>加载自定义内容</h3></header><article><p>通过定义<code>custom</code>属性可以更灵活的为对话框更新内容。</p><h4>指定内容文本</h4><p>最简单的方法是为custom指定内容文本即可。同样可以使用<code>data-custom</code>属性。</p><div class="example">
      <button type="button" class="btn btn-primary" data-custom="<h1>此内容是自定义的</h1><p>哈哈:)</p>" data-toggle="modal">指定内容文本</button>
    </div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="com">&lt;!-- 使用data-custom 属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-custom</span><span class="pun">=</span><span class="atv">"此内容是自定义的:)"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">指定内容文本</span><span class="tag">&lt;/button&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">/* 使用Javascript方法绑定在按钮上触发 */</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">modalTrigger</span><span class="pun">({</span><span class="pln">custom</span><span class="pun">:</span><span class="pln"> </span><span class="str">'&lt;h1&gt;此内容是自定义的&lt;/h1&gt;&lt;p&gt;哈哈:)&lt;/p&gt;'</span><span class="pun">});</span><span class="pln">

</span><span class="com">/* 使用触发器对象直接显示 */</span><span class="pln">
</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">({</span><span class="pln">custom</span><span class="pun">:</span><span class="pln"> </span><span class="str">'自定义内容'</span><span class="pun">})).</span><span class="pln">show</span><span class="pun">();</span></code></pre><h4>指定页面元素作为内容</h4><p>通过指定一个jQuery选择器名称来获取其内容作为对话框的内容。</p><div class="example">
  <div id="myModalAlert" class="alert alert-success with-icon">
    <i class="icon-ok"></i>
    <div class="content">我希望能够在对话框看到此消息。</div>
  </div>
  <button type="button" class="btn btn-primary" data-custom="#myModalAlert" data-toggle="modal">指定页面元素的内容</button>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="com">&lt;!-- 使用data-custom 属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-custom</span><span class="pun">=</span><span class="atv">"#myModalAlert"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">指定页面元素的内容</span><span class="tag">&lt;/button&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">/* 使用Javascript方法绑定在按钮上触发 */</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">modalTrigger</span><span class="pun">({</span><span class="pln">custom</span><span class="pun">:</span><span class="pln"> </span><span class="str">'#myModalAlert'</span><span class="pun">});</span><span class="pln">

</span><span class="com">/* 使用触发器对象直接显示 */</span><span class="pln">
</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">({</span><span class="pln">custom</span><span class="pun">:</span><span class="pln"> </span><span class="str">'#myModalAlert'</span><span class="pun">})).</span><span class="pln">show</span><span class="pun">();</span></code></pre><h4>使用jQuery对象作为内容</h4><p>直接为custom参数赋值为一个jQuery对象，此对象的内容将显示在对话框中。此方式无法使用data属性调用。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">/* 使用Javascript方法绑定在按钮上触发 */</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">modalTrigger</span><span class="pun">({</span><span class="pln">custom</span><span class="pun">:</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#myModalAlert'</span><span class="pun">)});</span><span class="pln">

</span><span class="com">/* 使用触发器对象直接显示 */</span><span class="pln">
</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">({</span><span class="pln">custom</span><span class="pun">:</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#myModalAlert'</span><span class="pun">)})).</span><span class="pln">show</span><span class="pun">();</span></code></pre><h4>使用回调函数动态生成内容</h4><p>将一个回调函数赋值给custom参数，触发器对象会自动调用此函数来实时获得动态内容并显示在对话框中。</p><p>回调函数提供一个参数对象，可以在生成内容时灵活运用。如果生成的内容是异步的时候，需要在内容准备就绪时调用参数提供的<code>.ready()</code>方法来显示对话框。</p><div class="example">
  <button type="button" class="btn btn-primary" id="modalTriggerByFunc">使用函数生成内容</button>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">/* 使用Javascript方法绑定在按钮上触发 */</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">modalTrigger</span><span class="pun">({</span><span class="pln">custom</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"当对话框显示时，时间是："</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Date</span><span class="pun">).</span><span class="pln">toString</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}});</span><span class="pln">

</span><span class="com">/* 使用触发器对象直接显示 */</span><span class="pln">
</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">({</span><span class="pln">custom</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    </span><span class="kwd">return</span><span class="pln"> </span><span class="str">"当对话框显示时，时间是："</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Date</span><span class="pun">).</span><span class="pln">toString</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}})).</span><span class="pln">show</span><span class="pun">();</span></code></pre><p>如果你的回调函数是全局的，则只需要将custom参数指定为该函数的名称的字符串。</p></article></section>




<section><header><h3>自动调整位置</h3></header><article><p>当对话框内的内容发生更改导致对话框尺寸发生改变时，对话框会根据设置自动调整位置。</p><p>自动调整位置适合任何形式的对话框。</p><div class="example">
  <button type="button" class="btn btn-primary" data-toggle="modal" data-custom="<div contenteditable='true' style='height: auto' class='form-control'>输入一些内容和换行来更改对话框大小</div>" id="modalTriggerByFunc">测试自动调整位置</button>
</div></article></section>



<section><header><h3>全屏对话框</h3></header><article><div class="example">
  <button type="button" class="btn btn-primary" data-custom="<h1>这是一个全屏对话框示例</h1>" data-toggle="modal" data-size="fullscreen">全屏对话框</button>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-custom</span><span class="pun">=</span><span class="atv">"..."</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="pln"> </span><span class="atn">data-size</span><span class="pun">=</span><span class="atv">"fullscreen"</span><span class="tag">&gt;</span><span class="pln">全屏对话框</span><span class="tag">&lt;/button&gt;</span></code></pre></article></section>



<section><header><h3>自定义头部</h3></header><article><h4>不显示头部</h4><p>不显示头部需要指定参数<code>showHeader</code>为<code>false</code>。</p><div class="example">
  <button type="button" class="btn btn-primary" data-toggle="modal" data-custom="这个对话框不显示头部" data-show-header="false" id="modalTriggerByFunc">不显示头部</button>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="com">&lt;!-- 使用data-showHeader 属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-show-header</span><span class="pun">=</span><span class="atv">"false"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">不显示头部</span><span class="tag">&lt;/button&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">/* 使用Javascript方法绑定在按钮上触发 */</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">modalTrigger</span><span class="pun">({</span><span class="pln">showHeader</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">});</span><span class="pln">

</span><span class="com">/* 使用触发器对象直接显示 */</span><span class="pln">
</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">({</span><span class="pln">showHeader</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">false</span><span class="pun">})).</span><span class="pln">show</span><span class="pun">();</span></code></pre><h4>自定义对话框标题</h4><p>如果不指定对话框的标题，会自动使用触发按钮的title属性或文本作为对话框的标题。</p><div class="example"><button type="button" class="btn btn-primary" data-toggle="modal" data-custom="此对话框的标题是新的" data-title="新的标题很重要" id="modalTriggerByFunc">自定义标题</button></div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="com">&lt;!-- 使用data-title 属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-title</span><span class="pun">=</span><span class="atv">"新的标题"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">不显示头部</span><span class="tag">&lt;/button&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">/* 使用Javascript方法绑定在按钮上触发 */</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">modalTrigger</span><span class="pun">({</span><span class="pln">title</span><span class="pun">:</span><span class="pln"> </span><span class="str">'新的标题'</span><span class="pun">});</span><span class="pln">

</span><span class="com">/* 使用触发器对象直接显示 */</span><span class="pln">
</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">({</span><span class="pln">title</span><span class="pun">:</span><span class="pln"> </span><span class="str">'新的标题'</span><span class="pun">})).</span><span class="pln">show</span><span class="pun">();</span></code></pre><h4>使用图标</h4><p>通过指定<code>icon</code>属性来在标题前面增加一个图标。</p><p>图标的定义请参考“控件-&gt;图标”章节。</p><div class="example"><button type="button" class="btn btn-primary" data-toggle="modal" data-custom="此对话框标题包含一个图标" data-icon="heart" data-title="图标很重要" id="modalTriggerByFunc">看看图标是什么</button></div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="com">&lt;!-- 使用data-title 属性 --&gt;</span><span class="pln">
</span><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-icon</span><span class="pun">=</span><span class="atv">"heart"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="tag">&gt;</span><span class="pln">不显示头部</span><span class="tag">&lt;/button&gt;</span></code></pre><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="com">/* 使用Javascript方法绑定在按钮上触发 */</span><span class="pln">
$</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">modalTrigger</span><span class="pun">({</span><span class="pln">icon</span><span class="pun">:</span><span class="pln"> </span><span class="str">'heart'</span><span class="pun">});</span><span class="pln">

</span><span class="com">/* 使用触发器对象直接显示 */</span><span class="pln">
</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">({</span><span class="pln">icon</span><span class="pun">:</span><span class="pln"> </span><span class="str">'heart'</span><span class="pun">})).</span><span class="pln">show</span><span class="pun">();</span></code></pre></article></section>




<section><header><h3>可拖动的对话框</h3></header><article><p>开启此选项可以允许用户在对话框显示之后通过拖拽头部移动对话框。完整例子参见 <a href="#javascript/modaltrigger/5">JS 插件 -&gt; 对话框</a>。</p><div class="example">
  <button type="button" class="btn btn-primary" data-icon="move" data-moveable="true" data-toggle="modal" data-custom="<div><p>拖动我的头部来移动此对话框。</p><h1>:)</h1></div>" id="modalTriggerMoveable"><i class="icon icon-move"></i> 打开我 拖动我</button>
</div><pre class="prettyprint prettyprinted" style=""><code class="lang-html"><span class="tag">&lt;button</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"button"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"btn btn-primary"</span><span class="pln"> </span><span class="atn">data-moveable</span><span class="pun">=</span><span class="atv">"true"</span><span class="pln"> </span><span class="atn">data-toggle</span><span class="pun">=</span><span class="atv">"modal"</span><span class="pln"> </span><span class="atn">data-custom</span><span class="pun">=</span><span class="atv">"..."</span><span class="tag">&gt;&lt;i</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"icon icon-move"</span><span class="tag">&gt;&lt;/i&gt;</span><span class="pln"> 打开我 拖动我</span><span class="tag">&lt;/button&gt;</span></code></pre></article></section>

<style>
.modal > .loader {
  opacity: 1;
  height: auto;
  transform: scale(1);
}
</style>

<script>
$(function(){
  afterPageLoad();
});
function afterPageLoad() {
    $(document).on('click', '.modal-backdrop.in, .modal', function(e) {
        e.stopPropagation();
    });

    /* modal trigger */
    var $trigger = $('#modalTriggerByFunc');
    if(!$trigger.data('modalTriggerSetup')) {
        $trigger.modalTrigger({moveable: true, custom: function() {
            return "当对话框显示时，时间是：" + (new Date).toString();
        }});
        $trigger.data('modalTriggerSetup', 1)
    }
}
</script><section><header><h3>如何使用</h3></header><article><h4>使用data属性来调用</h4><p>使用在触发按钮上设置data属性即可使用。此方法与常规对话框触发按钮属性书写方式完全一致，只不过需要增加几个特殊属性，包括<code>data-remote</code>、<code>data-iframe</code>和<code>data-custom</code>。</p><p>使用dta属性来调用请参考上面的实例章节。</p><h4>使用Javascript手动调用</h4><p>Javascript方法也十分灵活。</p><h4>jQuery对象方法</h4><p>使得一个jquery对象能够监听事件（一般为点击）并启动对话框。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">modalTrigger</span><span class="pun">(</span><span class="pln">options</span><span class="pun">);</span></code></pre><p>也可以使用<code>modalTrigger</code>的别名方法<code>modal</code>，这样与传统对话框的初始化方法完全一致。</p><h4>使用预设的modalTrigger实例</h4><p>在<code>$.zui</code>对象上已默认绑定了一个对话框触发器对象，可以直接使用<code>方法</code>并传递不同的参数来随时启动对话框。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="pln">modalTrigger</span><span class="pun">.</span><span class="pln">show</span><span class="pun">(</span><span class="pln">options</span><span class="pun">);</span></code></pre><h4>创建新的触发器对象</h4><p>创建一个新的对话框触发器来保存配置并启动对话框。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="kwd">var</span><span class="pln"> myModalTrigger </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">(</span><span class="pln">options</span><span class="pun">);</span><span class="pln">
myModalTrigger</span><span class="pun">.</span><span class="pln">show</span><span class="pun">();</span></code></pre><h4>参数</h4><p>在初始化对话框或者显示时都能够使用参数来个性化你的对话框。</p><ul>
          <li><code>'fit'</code>：最佳位置（默认）；</li>
          <li><code>'center'</code>：显示在窗口中间；</li>
          <li><code>'0'</code>：显示在最顶部；</li>
          <li><code>'200'</code>：数字用来指定距离顶部的像素；</li>
          <li>CSS支持的所有表示位置的值，用来指定距离顶部的偏移；</li>
          <li>使用一个回调函数来动态返回每次显示的位置（使用 JS 表示的样式的对象）。</li>
        </ul><div class="table-responsive"><table class="table table-bordered">
  <thead>
    <tr>
      <th>参数</th>
      <th>名称</th>
      <th>可选值</th>
      <th>说明</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>name</code></td>
      <td>对话框元素名称</td>
      <td>可选，默认为 <code>'triggerModal'</code></td>
      <td>该名称会作为内部表示此触发器实例使用，并且会作为最终生成的 <code>.modal</code> 元素的 ID 属性</td>
    </tr>
    <tr>
      <td><code>className</code></td>
      <td>对话框元素类名</td>
      <td>可选，默认为 <code>''</code></td>
      <td>添加到最终生成的 <code>.modal</code> 元素的 CLASS 属性上</td>
    </tr>
    <tr>
      <td><code>type</code></td>
      <td>对话框类型</td>
      <td><em>   <code>'custom'</code>（默认），自定义类型
</em>   <code>'iframe'</code>
<em>   <code>'ajax'</code></em></td>
      <td>通常该参数和参数<code>url</code>一起使用，当指定了<code>custom</code>、<code>remote</code>和<code>iframe</code>参数时该参数可以忽略</td>
    </tr>
    <tr>
      <td><code>url</code></td>
      <td>远程内容地址</td>
      <td>远程地址字符串</td>
      <td>通常该参数和<code>type</code>一起似乎用，当指定了<code>custom</code>、<code>remote</code>和<code>iframe</code>参数时该参数可以忽略</td>
    </tr>
    <tr>
      <td><code>remote</code></td>
      <td>Ajax内容地址</td>
      <td>远程地址字符串</td>
      <td>如果使用该参数，则参数<code>type</code>和<code>url</code>可以忽略</td>
    </tr>
    <tr>
      <td><code>iframe</code></td>
      <td>iframe页面地址</td>
      <td>远程地址字符串</td>
      <td>如果使用该参数，则参数<code>type</code>和<code>url</code>可以忽略</td>
    </tr>
    <tr>
      <td><code>size</code></td>
      <td>对话框大小</td>
      <td>   <code>''</code>（默认），默认大小
<em>   <code>'lg'</code>，大对话框
</em>   <code>'sm'</code>，小对话框
<em>   <code>'fullscreen'</code>，全屏显示</em></td>
      <td></td>
    </tr>
    <tr>
      <td><code>width</code></td>
      <td>对话框宽度</td>
      <td>   <code>null</code>（默认），默认宽度
<em>   其他表示宽度的CSS值字符串</em></td>
      <td>如果使用<code>size</code>参数，则可以忽略该参数</td>
    </tr>
    <tr>
      <td><code>height</code></td>
      <td>对话框高度</td>
      <td>   <code>'auto'</code>（默认），自动根据内容调整
<em>   其他表示高度的CSS值字符串</em></td>
      <td>如果指定了高度不是<code>'auto'</code>则可能出现内容与高度不匹配的情况。</td>
    </tr>
    <tr>
      <td><code>showHeader</code></td>
      <td>是否显示标题</td>
      <td>   <code>true</code>（默认）
<em>   <code>false</code></em></td>
      <td></td>
    </tr>
    <tr>
      <td><code>title</code></td>
      <td>对话框标题文本</td>
      <td>字符串</td>
      <td>当参数<code>showHeader</code>为<code>false</code>则此参数无效。</td>
    </tr>
    <tr>
      <td><code>icon</code></td>
      <td>对话框标题图标</td>
      <td>图标名称字符串</td>
      <td>当参数<code>showHeader</code>为<code>false</code>则此参数无效。</td>
    </tr>
    <tr>
      <td><code>fade</code></td>
      <td>是否使用淡入淡出动画</td>
      <td>   <code>true</code>（默认）
<em>   <code>false</code></em></td>
      <td></td>
    </tr>
    <tr>
      <td><code>position</code></td>
      <td>对话框位置</td>
        
      <td>最佳位置在窗口中间稍偏上的位置。</td>
    </tr>
    <tr>
      <td><code>backdrop</code></td>
      <td>背景遮罩</td>
      <td>   <code>true</code>（默认）
<em>   <code>false</code>
</em>   <code>'static'</code></td>
      <td>使用布尔值来启用或禁用背景遮罩，如果指定为<code>'static'</code>则会启动背景遮罩，但点击背景遮罩时不会触发关闭对话框的过程。</td>
    </tr>
    <tr>
      <td><code>keyboard</code></td>
      <td>按键</td>
      <td><em>   <code>true</code>（默认）
</em>   <code>false</code></td>
      <td>当为<code>ture</code>时，按下<code>esc</code>键会关闭对话框。</td>
    </tr>
    <tr>
      <td><code>moveable</code></td>
      <td>可移动的</td>
      <td>
        <ul>
          <li><code>false</code>：不启用（默认）；</li>
          <li><code>true</code>：启用；</li>
          <li><code>'inside'</code>：启用并限制对话框只能移动到窗口内部。</li>
        </ul>
      </td>
      <td>是否启用对话框拖拽移动功能</td>
    </tr>
    <tr>
      <td><code>rememberPos</code></td>
      <td>记住移动的位置</td>
      <td><em>   <code>false</code>（默认），不记住位置；
</em>   <code>true</code>，记住位置；
<em>   页面内值唯一的字符串，使用本地存储记住位置；</em></td>
      <td>启用该选项需要同时启用 <code>moveable</code> 选项，当该值为一个在页面范围内值唯一的字符串时，通过浏览器本地存储来存储数据，关闭页面或浏览器之后也不会忘记。</td>
    </tr>
    <tr>
      <td><code>waittime</code></td>
      <td>加载远程内容时的最大等待时间</td>
      <td>整数，代表等待的毫秒数，默认为<code>0</code></td>
      <td>在指定的时间之后会直接显示对话框，不管远程内容是否加载完毕；默认值为<code>0</code>，表示一直等待直到远程内容加载完毕才显示对话框。在等待时会显示正在加载的动画。</td>
    </tr>
    <tr>
      <td><code>loadingIcon</code></td>
      <td>加载时显示的动画所使用的图标</td>
      <td>   以<code>icon-*</code>形式定义的图标名称
<em>   自定义加载动画所使用的html</em></td>
      <td>`icon-<code>定义的图标名称请参考[图标](#search/icon:spin)。&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;</code>scrollInside<code>&lt;/td&gt;
      &lt;td&gt;是否在对话框内部显示滚动条&lt;/td&gt;
      &lt;td&gt;默认为</code>false`，即在对话框所属的页面上显示滚动条</td>
      <td></td>
    </tr>
  </tbody>
</table></div><h4>方法</h4><h4>获取触发器对象实例</h4><p>要使用触发器方法，需要首先获取触发器对象的实例。</p><h6>通过触发按钮的data属性</h6><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="kwd">var</span><span class="pln"> modalTrigger </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">(</span><span class="str">'#triggerButton'</span><span class="pun">).</span><span class="pln">data</span><span class="pun">(</span><span class="str">'zui.modaltrigger'</span><span class="pun">);</span></code></pre><h6>使用预设的$.zui对象绑定的触发器</h6><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="kwd">var</span><span class="pln"> myTrigger </span><span class="pun">=</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="pln">modalTrigger</span><span class="pun">;</span></code></pre><h6>创建新的触发器实例</h6><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="kwd">var</span><span class="pln"> newTrigger </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> $</span><span class="pun">.</span><span class="pln">zui</span><span class="pun">.</span><span class="typ">ModalTrigger</span><span class="pun">(</span><span class="pln">options</span><span class="pun">);</span></code></pre><h4>显示对话框</h4><p>在显示对话框时，可以重新传入新的参数，而不影响触发器对象原来的参数。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">myModalTrigger</span><span class="pun">.</span><span class="pln">show</span><span class="pun">(</span><span class="pln">options</span><span class="pun">);</span></code></pre><h4>关闭对话框</h4><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">myModalTrigger</span><span class="pun">.</span><span class="pln">close</span><span class="pun">();</span></code></pre><h4>切换显示和关闭状态</h4><p>如果对话框没有显示，则立即显示，如果已经显示则关闭。</p><p>在切换显示和关闭状态时，可以重新传入新的参数，而不影响触发器对象原来的参数。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">myModalTrigger</span><span class="pun">.</span><span class="pln">toggle</span><span class="pun">(</span><span class="pln">options</span><span class="pun">);</span></code></pre><h4>重新调整位置</h4><p>使用新的位置参数来重新设置对话框的显示位置。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">myModalTrigger</span><span class="pun">.</span><span class="pln">ajustPosition</span><span class="pun">(</span><span class="pln">options</span><span class="pun">);</span></code></pre><h4>事件</h4><p>因为对话框的DOM内容是动态生成的，不方便使用jQuery方法来绑定事件，不过可以将监听事件的回调函数传入参数中。</p><div class="table-responsive"><table class="table table-bordered">
  <thead>
    <tr>
      <th>事件</th>
      <th>jquery事件名称</th>
      <th>说明</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>onShow</code></td>
      <td><code>show.zui.modal</code></td>
      <td>当对话框的show方法被调用时立即发生</td>
    </tr>
    <tr>
      <td><code>shown</code></td>
      <td><code>shown.zui.modal</code></td>
      <td>当对话框完全显示后发生（执行完显示动画之后）</td>
    </tr>
    <tr>
      <td><code>onHide</code></td>
      <td><code>hide.zui.modal</code></td>
      <td>当对话框hide方法被调用是立即发生</td>
    </tr>
    <tr>
      <td><code>hidden</code></td>
      <td><code>hidden.zui.modal</code></td>
      <td>当对话框完全隐藏后发生（执行完隐藏动画之后）</td>
    </tr>
    <tr>
      <td><code>loaded</code></td>
      <td><code>loaded.zui.modal</code></td>
      <td>当远程内容加载完成后发生</td>
    </tr>
    <tr>
      <td><code>broken</code></td>
      <td><code>broken.zui.modal</code></td>
      <td>当加载远程内容失败之后调用，在次回调方法中返回字符串会更新对话框现实的内容（例如在对话框中显示一个加载失败之后的帮助信息）。</td>
    </tr>
  </tbody>
</table></div><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">myModalTrigger</span><span class="pun">.</span><span class="pln">show</span><span class="pun">({</span><span class="pln">shown</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
    alert</span><span class="pun">(</span><span class="str">'对话框已显示。'</span><span class="pun">);</span><span class="pln">
</span><span class="pun">}});</span></code></pre><p>如果对话框的<code>name</code>参数是已知的，则可以获取对话框的jquery对象实例通过jQuery对象的on方法来绑定事件。</p><pre class="prettyprint prettyprinted" style=""><code class="lang-js"><span class="pln">$</span><span class="pun">(</span><span class="str">'#triggerModal'</span><span class="pun">).</span><span class="pln">on</span><span class="pun">(</span><span class="str">'shown.zui.modal'</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">function</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{...});</span></code></pre></article></section></div>
</section>
{/block}